//
//  AddressTableViewCell.m
//  ForestPack
//
//  Created by 郑洲 on 2018/7/9.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "AddressTableViewCell.h"
#import "AddressManagerModel.h"

@interface AddressTableViewCell ()

@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *addressDetail;
@property (nonatomic, strong) UIImageView *line;
@property (nonatomic, strong) UIButton *selectBtn;
@property (nonatomic, strong) UIButton *editBtn;
@property (nonatomic, strong) UIButton *deleteBtn;

@end

@implementation AddressTableViewCell

+ (AddressTableViewCell *)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"AddressTableViewCell";
    AddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[AddressTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        [cell createSubView];
    }
    
    return cell;
}

- (void)createSubView {
    _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(16), 12 * SCREEN_WIDTH / 375, SCREEN_WIDTH - 30 * SCREEN_WIDTH / 375, 22 * SCREEN_WIDTH / 375)];
    _nameLabel.textColor = FONTCOLOR_BLACK;
    _nameLabel.font = Kfont(16);
    [self addSubview:_nameLabel];
    
    _addressDetail = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(16), 44 * SCREEN_WIDTH / 375, SCREEN_WIDTH - 32 * SCREEN_WIDTH / 375, 15 * SCREEN_WIDTH / 375)];
    _addressDetail.font = [UIFont systemFontOfSize:14 * SCREEN_WIDTH / 375];
    _addressDetail.textColor = REXADECIMALCOLOR(@"#999999");
    [self addSubview:_addressDetail];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenX375(80), SCREEN_WIDTH, 0.5)];
    line.backgroundColor = REXADECIMALCOLOR(@"#f5f5f5");
    [self addSubview:line];

    _selectBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(16), 91 * SCREEN_WIDTH / 375, 91 * SCREEN_WIDTH / 375, 18 * SCREEN_WIDTH / 375)];
    [_selectBtn setTitle:@" 设为默认" forState:UIControlStateNormal];
    [_selectBtn setTitleColor:FONTCOLOR_LIGHTGRAY forState:UIControlStateNormal];
    _selectBtn.titleLabel.font = [UIFont systemFontOfSize:13 * SCREEN_WIDTH / 375];
    [_selectBtn setImage:[UIImage imageNamed:@"设为默认"] forState:UIControlStateNormal];
    _selectBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [_selectBtn addTarget:self action:@selector(selectClicked) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_selectBtn];
    
    _editBtn = [[UIButton alloc] initWithFrame:CGRectMake(250 * SCREEN_WIDTH / 375, 91 * SCREEN_WIDTH / 375, 50 * SCREEN_WIDTH / 375, 18 * SCREEN_WIDTH / 375)];
    [_editBtn setTitle:@" 编辑" forState:UIControlStateNormal];
    [_editBtn setTitleColor:FONTCOLOR_LIGHTGRAY forState:UIControlStateNormal];
    _editBtn.titleLabel.font = [UIFont systemFontOfSize:13 * SCREEN_WIDTH / 375];
    [_editBtn setImage:[UIImage imageNamed:@"编辑"] forState:UIControlStateNormal];
    [_editBtn addTarget:self action:@selector(editClicked) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_editBtn];
    
    _deleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(314 * SCREEN_WIDTH / 375, 91 * SCREEN_WIDTH / 375, 50 * SCREEN_WIDTH / 375, 18 * SCREEN_WIDTH / 375)];
    [_deleteBtn setTitle:@" 删除" forState:UIControlStateNormal];
    [_deleteBtn setTitleColor:FONTCOLOR_LIGHTGRAY forState:UIControlStateNormal];
    _deleteBtn.titleLabel.font = [UIFont systemFontOfSize:13 * SCREEN_WIDTH / 375];
    [_deleteBtn setImage:[UIImage imageNamed:@"删除的副本"] forState:UIControlStateNormal];
    [_deleteBtn addTarget:self action:@selector(deleteClicked) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_deleteBtn];
}

- (void)selectClicked {
    if (_selectClick) {
        _selectClick();
    }
}

- (void)setAddressModel:(AddressManagerModel *)addressModel {
    _addressModel = addressModel;
    
    _nameLabel.text = [NSString stringWithFormat:@"%@    %@",addressModel.recName,[addressModel.recPhone stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"]];
    _addressDetail.text = [NSString stringWithFormat:@"%@%@%@%@",addressModel.recProvince,addressModel.recCity,addressModel.recArea,addressModel.recAddress];
    if ([self.addressModel.isDefault isEqualToString:@"0"]) {
        [_selectBtn setTitle:@" 设为默认" forState:UIControlStateNormal];
        [_selectBtn setImage:[UIImage imageNamed:@"设为默认"] forState:UIControlStateNormal];
    }else{
        [_selectBtn setTitle:@" 默认地址" forState:UIControlStateNormal];
        [_selectBtn setImage:[UIImage imageNamed:@"选择"] forState:UIControlStateNormal];
    }
}

- (void)editClicked {
    if (_editClick) {
        _editClick();
    }
}

- (void)deleteClicked {
    if (_deleteClick) {
        _deleteClick();
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end


